package com.example.its2021606134chh.BusDiyModule;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.its2021606134chh.BusModule.ExpandableListviewAdapter;
import com.example.its2021606134chh.MainActivity;
import com.example.its2021606134chh.R;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link BusDiyFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class BusDiyFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "title";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private ImageView imgDiyBusBack;
    private ImageView imgBusDiyQRCode;
    private TextView tvDiyBusOrder;
    private ExpandableListView expandListDiyBus;

    public BusDiyFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment BusDiyFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static BusDiyFragment newInstance(String param1, String param2) {
        BusDiyFragment fragment = new BusDiyFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_bus_diy, container, false);
        imgDiyBusBack = (ImageView) view.findViewById(R.id.imgDiyBusBack);
        imgBusDiyQRCode = (ImageView) view.findViewById(R.id.imgBusDiyQRCode);
        tvDiyBusOrder = (TextView) view.findViewById(R.id.tvDiyBusOrder);
        expandListDiyBus = (ExpandableListView) view.findViewById(R.id.expand_list_diyBus);
        imgDiyBusBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), MainActivity.class);
                startActivity(intent);
                getActivity().finish();
            }
        });
        imgBusDiyQRCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), BusDiyQRCodeActivity.class);
                startActivity(intent);
            }
        });
        tvDiyBusOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), DiyBusOrderActivity.class);
                startActivity(intent);
            }
        });
        // Inflate the layout for this fragment
        //        类似于RecycleView
        List<BusDiyData> groups = new ArrayList<>();
        groups.add(new BusDiyData("1号线", "光谷金融街", "南湖商厦", "06:45", "19:45", "06:55", "19:55", "8"));
        groups.add(new BusDiyData("2号线", "光谷金融街", "南湖商厦", "06:45", "19:45", "06:55", "19:55", "8"));
        String[][] childs = new String[][]{{"光谷金融街", "荣军路南", "南湖商厦"}, {"光谷金融街", "荣军路南", "南湖商厦"}};
        DiyExpandableListviewAdapter adapter = new DiyExpandableListviewAdapter(getContext(), groups, childs);
        expandListDiyBus.setAdapter(adapter);
        //默认展开第一个数组
        expandListDiyBus.expandGroup(0);
//        这个方法卡顿严重，需要优化
        expandListDiyBus.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Intent intent = new Intent(getContext(), BusMapShowActivity.class);
                intent.putExtra("id", "1");
                intent.putExtra("start_place", "光谷金融街");
                intent.putExtra("end_place", "南湖商厦");
                intent.putExtra("price", "8");
                startActivity(intent);
                getActivity().finish();
                return true;
            }
        });
        return view;
    }

    private void initView() {

    }
}